package com.rooster.goods.feign;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.rooster.goods.entity.Goods;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @Author C_Y_J
 * @create 2021/10/23 13:54
 **/
@RestController
@RequestMapping("/goods")
public class GoodsFeignService {
    /**
     * fallbackMethod：指定降级后调用的方法名称
     * commandProperties 设置Hystrix的超时时间，默认1s
     * 在启动类上开启Hystrix功能：@EnableCircuitBreaker
     *
     * @return
     */
    @GetMapping("/findOne1")
    @HystrixCommand(fallbackMethod = "findGoodsById1_fallback",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")})
    public Goods findGoodsById1() {
        // 当前线程睡2秒
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne1");
        return goods;
    }

    public Goods findGoodsById1_fallback() {
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne1：因为商品服务端延迟，商品服务端自动降级");
        return goods;
    }

    @GetMapping("/findOne2")
    @HystrixCommand(fallbackMethod = "findGoodsById2_fallback")
    public Goods findGoodsById2() {
        // 手动制造异常
        int i = 1 / 0;
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne2");
        return goods;
    }

    public Goods findGoodsById2_fallback() {
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne2：因为商品服务端异常，商品服务端自动降级");
        return goods;
    }

    /**
     * ================================== 下面的不在服务提供方进行降级,在消费方进行降级。==================================
     */

    @GetMapping("/findOne3")
    public Goods findGoodsById3() {
        // 当前线程睡2秒
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne3");
        return goods;
    }

    @GetMapping("/findOne4")
    public Goods findGoodsById4() {
        // 手动制造异常
        int i = 1 / 0;
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne4");
        return goods;
    }

    /**
     * ================================== 正常使用 ==================================
     */

    @GetMapping("/findOne5")
    public Goods findGoodsById5() {
        Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("findOne5");
        return goods;
    }
}
